<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class JobController extends Controller
{
    //职业分类
    public function joblist(){
        $res = DB::table('jobs')->get();
//        dd($res);
        return view('admin/jobs/jobs',['jobs'=>$res]);
    }

    public function Jobadd(){
        return view('admin/jobs/jobsadd');
    }

    public function jobadddata(Request $request){
        $name = $request->name;
        $jid = $request->jid;
        $res = DB::table('jobs')->insert(['jobname'=>$name,'jid'=>$jid]);
        return redirect('admin/joblist');
    }

    public function jobedit(){
        $id = $_GET['id'];
        $res = DB::table('jobs')->where('id',$id)->get();
        $rec = DB::table('jobs')->get();
        return view('admin/jobs/jobsedit',['res'=>$res,'rec'=>$rec]);

    }

    public function jobeditdata(Request $request){
        $id = $request->id;
        $name = $request->name;
        $jid = $request->jid;
        $res = DB::table('jobs')->where('id',$id)->update(['jobname'=>$name,'jid'=>$jid]);
        return redirect('admin/joblist');
    }

    public function jobdel(){
        $id = $_GET['id'];
        $rec = DB::table('jobs')->where('jid',$id)->get();
        if ($rec != false){
            $res = DB::table('jobs')->where('id',$id)->delete();
            return redirect('admin/joblist');
        }else{
            return redirect('admin/joblist');
        }

    }
    //职业管理
    public function sjobadd(){
        $rec = DB::table('jobs')->get();
        return view('admin/job/jobadd',['rec'=>$rec]);
    }

    public function sjobadddata(Request $request){
        $name = $request->name;
        $sid = $request->sid;
        $res = DB::table('job')->insert(['sjobname'=>$name,'jobs_id'=>$sid]);
//        dd($res);
        return redirect('admin/sjoblist');
    }

    public function sjoblist(){
        $res = DB::table('job')->join('jobs','jobs.id','job.jobs_id')
            ->select('job.*','jobs.jobname')->get();
//        dd($res);
        return view('admin/job/joblist',['job'=>$res]);
    }

    public function sjobedit(){
        $id = $_GET['id'];
        $res = DB::table('job')->where('id',$id)->get();
        $rec = DB::table('jobs')->get();
        return view('admin/job/jobedit',['res'=>$res,'rec'=>$rec]);
    }

    public function sjobeditdata(Request $request){
        $id = $request->id;
        $name = $request->name;
        $jid = $request->jid;
        $res = DB::table('job')->where('id',$id)->update(['sjobname'=>$name,'jobs_id'=>$jid]);
        return redirect('admin/sjoblist');
    }

    public function sjobdel(){
        $id = $_GET['id'];
        $res = DB::table('job')->where('id',$id)->delete();
        return redirect('admin/sjoblist');

    }

//职业描述
    public function jobneed(){
        $res = DB::table('jobneed')->join('job','job.id','jobneed.sjob_id')->select('jobneed.*','job.sjobname')->get();
        return view('admin/jobneed/jobneedlist',['job'=>$res]);
    }

    public function needadd(){
        $res = DB::table('job')->get();
        return view('admin/jobneed/jobneedadd',['res'=>$res]);
    }
    public function needadddata(Request $request){
        $jobneed = $request->jobneed;
        $jid = $request->jid;
        $res = DB::table('jobneed')->insert(['jobneed'=>$jobneed,'sjob_id'=>$jid]);
        if ($res != false){
            return redirect('admin/job_need');
        }
    }


    public function neededit(){
        $id = $_GET['id'];
        $res = DB::table('jobneed')->where('id',$id)->get();
        $rec = DB::table('job')->get();
        return view('admin/jobneed/jobneededit',['res'=>$res,'rec'=>$rec]);
    }


    public function neededitdata(Request $request){
        $id = $request->id;
        $jobneed = $request->jobneed;
        $jid = $request->jid;
        $res = DB::table('jobneed')->where('id',$id)->update(['jobneed'=>$jobneed,'sjob_id'=>$jid]);
        return redirect('admin/job_need');
    }

    public function needdel(){
        $id = $_GET['id'];
        $res = DB::table('jobneed')->where('id',$id)->delete();
        return redirect('admin/job_need');

    }
}
